package com.ljtxxhwj.myblob.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ljtxxhwj.myblob.entity.*;
import com.ljtxxhwj.myblob.mapper.ArticleHasTagMapper;
import com.ljtxxhwj.myblob.mapper.ArticleMapper;
import com.ljtxxhwj.myblob.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    ArticleMapper articleMapper;

    @Autowired
    ArticleHasTagMapper articleHasTagMapper;

    @Override
    public List<Article> getAll() {
        return articleMapper.select();
    }

    @Override
    public int getTotalSeeByUserId(int userId) {
        return articleMapper.getTotalSeeByUserId(userId);
    }
    @Override
    public boolean deleteOne(int id) {
        return articleMapper.delete(id);
    }
    @Override
    public List<Article> getArticlesByUserId(int userId) {
        return articleMapper.getArticlesByUserId(userId);}
    @Override
    public boolean add(Article article) {
        List<ArticleTag> articleTagList = article.getArticleTag();
        article.setArticleSee(0);
        articleMapper.insert(article);
        for (ArticleTag articleTag : articleTagList) {
            ArticleHasTag articleHasTag = new ArticleHasTag();
            articleHasTag.setArticleId(article.getArticleId());
            articleHasTag.setTagId(articleTag.getArticleTagId());
            System.out.println("articleHasTag = " + articleHasTag);
            articleMapper.insertTag(articleHasTag);
        }
        return true;
    }

    @Override
    public boolean modify(Article article) {
        return articleMapper.update(article);
    }

    @Override
    public Article getOne(int articleId) {
        return articleMapper.getOneByArticleId(articleId);
    }

    @Override
    public Article readOne(int articleId) {
        return articleMapper.getOneByArticleId(articleId);
    }

    @Override
    public List<ArticleTag> getTag(int articleId) {
        return articleMapper.getArticleTagByArticleId(articleId);
    }

    @Override
    public List<Comment> getComment(int articleId) {
        return  articleMapper.getCommentByArticleId(articleId);
    }

    @Override
    public boolean addComment(Comment comment) {
        return articleMapper.insertComment(comment);
    }

    @Override
    public boolean deleteComment(int commentId) {
        return articleMapper.deleteComment(commentId);
    }

    @Override
    public List<ArticleKind> getKind() {
        return articleMapper.getKind();
    }
    @Override
    public List<Article> findAllArticle() {
        List<Article> articleList=articleMapper.selectByExample(null);

        return articleList;
    }

    @Override
    public List<Article> findByTitle(String articleTitle) {
        ArticleExample example=new ArticleExample();
        example.createCriteria().andArticleTitleLike("%"+articleTitle+"%");
        List<Article> articleTitleList= articleMapper.selectByExample(example);
        return articleTitleList;
    }

    @Override
    public PageInfo<ArticleWithTypeAndUname> loadWithPage(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<ArticleWithTypeAndUname>  list = articleMapper.loadWithPage();
        PageInfo<ArticleWithTypeAndUname> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public int deleteBatch(int id) {
        return articleMapper.deleteBatch(id);
    }

    @Override
    public void deleteByArticleId(Integer articleId) {
        //删除标签关系
        articleMapper.deleteArticletTagByArticleId(articleId);
//        ArticleExample example=new ArticleExample();
//        example.createCriteria().andArticleIdEqualTo(articleId);
//        articleMapper.deleteByExample(example);

        //删除文章
        articleMapper.deleteByPrimaryKey(articleId);
    }


    @Override
    public List<Article> getWant(Article article) {
        return articleMapper.selectSelective(article);
    }

    @Override
    public List<Article> getWantAndNew(Article article) {
        return articleMapper.selectSelectiveAndNew(article);
    }
}
